 function [xcell,xm] = out_sqmat( xmat , x1 , x2, xname , pvec , titulo );
% =========================================================================
% OUT_SQMAT  
%
% function xcell = out_sqmat( xmat , x1 , x2, xname , pvec,  titulo );
% 
% Create an output cell where EITHER  xmat is a [nz nz ndr] matrix 
% or the median and the percentiles have BEEN ALREADY EXTRACTED and passed
% on as XMAT(median) X1(p1) and X2(p2) [nz nz] matrices
% 
% If the matrix is 3D and X1 is empty then median and percentiles
% automatically computed
% 
% Inputs 
% ------
% XNAME     Cell with names of rows/cols (square matrix) 
% PVEC      2x1 vector of percentiles used (0,1)
%           Default is [0.05 0.95]
% TITULO    Cell or string title for the cell 
% 
% Outputs 
% -------
% XCELL     Output cell 
%           Ser1        Ser2
% Ser1      Median      Median
%           [5,95]      [5,95] 
% 
% XM         Structure with fields MED P1 and P2
%            Empty if XMED,X1 and X2 provided 
%
% Alejandro Justiniano  9/6/05
% =============================================================
nz = size( xmat ); 

% =====================================================================
% Determine whether need to compute medians and percentiles or already
% provided 
% =====================================================================
if nargin < 6 
    error('Must provide 6 inputs') 
end 

flag_rawm = 0; 

if length( nz ) == 3 
    if isempty( x1 ) == 1
        flag_rawm = 1;
        disp('Computing Moments of Matrix in OUT_SQMAT.m');
    else
        error('Matrix cannot be 3D IF provide moments')
    end
end

% =======================
% Compute Matrix Moments 
% =======================
if flag_rawm == 1
    if isempty( pvec ) == 1
        pvec = [0.05;0.95];
        pind =round( nz(3)*pvec );
    end
    xmed = squeeze( median(xmat,3) );
    xmat= sort( xmat, 3 );
    x1 =  squeeze( xmat(:,:,pind(1) ) );
    x2 = squeeze( xmat(:,:,pind(2) ) );
    clear xmat pind; 
    
else

    xmed = xmat;
    clear xmat;

    if any( nz ~= size( x1 ) )
        error('Mistmatch x1')
    end

    if any( nz ~= size( x2 ) )
        error('Mistmatch x2')
    end

    if any( x1 > x2 ) == 1
        error('X1 must be a lower percentile than X2')
    end
    
    if length( pvec ) ~= 2 
        error('Pvec must be 2x1'); 
    end; 
    
    
end


nz = nz(1); 
 
xname = xname(:); 

if length( xname ) ~= nz 
    error('Mistmatch cell name') 
end 


xcell = emptycell( 2*nz + 2 , nz +1 ); 
xcell(1,1) = cellstr(titulo);
xcell(2,1) = {'Series (Med)'}; 
xcell(2,2:end) = xname'; 

ind = 3:2:(2*nz+1); 

temp_str = { ['[',num2str(pvec(1)*100),'%,',num2str(pvec(2)*100),'%]'] }; 

ii=1; 
for ii=1:nz; 
     
    row = ind(ii); 
    xcell(row,1) = xname(ii); 
    
    if ii==1
        xcell(row+1,1) = temp_str;
    end

    jj=1; 
    for jj=1:nz; 
        xcell(row,jj+1) = { sprintf( '    %5.3f', xmed(ii,jj) ) }; 
        temp_1 = sprintf( '%5.3f', x1(ii,jj) ) ; 
        temp_2 = sprintf( '%5.3f', x2(ii,jj) ) ; 
        xcell(row+1,jj+1) = { ['[',temp_1 ,',',temp_2,']'] }; 
    end 
end 

if flag_rawm == 1 
    xm.med = reshape( xmed , [nz nz] ) ; 
    xm.p1 = reshape( x1 , [nz nz] ) ; 
    xm.p2 = reshape( x2 , [nz nz] ) ; 
else 
    xm =[]; 
end 








